﻿Public Class u_NhaCungCap
    Private Sub LoadData()
        Dim cc As New NhaCungCapBUS
        dgvCC.DataSource = cc.GetNhaCungCap
        Dim tv As New NhomBUS
        cbxNhom.DataSource = tv.GetNhomNL
        cbxNhom.DisplayMember = "TenNhomNL"
        cbxNhom.ValueMember = "MaNhomNL"
        cbxNhom.SelectedIndex = 0
    End Sub

    Private Sub u_NhaCungCap_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadData()
    End Sub


    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim newCC As New NhaCungCapDTO
        newCC.MaCC = txtMa.Text
        newCC.TenCC = txtTen.Text
        newCC.NhomNguyenLieu = cbxNhom.SelectedItem
        newCC.DiaChi = txtDiaChi.Text
        newCC.SDT = txtsdt.Text
        Dim ccBUS As New NhaCungCapBUS
        ccBUS.AddNhaCungCap(newCC)
        LoadData()
    End Sub


    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim delCC As New NhaCungCapDTO
        delCC.STT = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "STT")
        Dim ccBUS As New NhaCungCapBUS
        ccBUS.DelNhaCungCap(delCC)
        LoadData()
    End Sub

    Private Sub btnCapNhat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCapNhat.Click
        Dim newCC As New NhaCungCapDTO
        newCC.STT = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "STT")
        newCC.MaCC = txtMa.Text
        newCC.TenCC = txtTen.Text
        newCC.NhomNguyenLieu = cbxNhom.Text
        newCC.SDT = txtsdt.Text
        newCC.DiaChi = txtDiaChi.Text
        Dim ccBUS As New NhaCungCapBUS
        ccBUS.UpdateNhaCungCap(newCC)
        LoadData()
    End Sub


    Private Sub gvCC_FocusedRowChanged(ByVal sender As System.Object, ByVal e As DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs) Handles gvCC.FocusedRowChanged
        txtMa.Text = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "MaNhaCungCap")
        txtTen.Text = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "TenNhaCungCap")
        txtDiaChi.Text = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "DiaChi")
        txtsdt.Text = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "SoDienThoai")
        cbxNhom.Text = gvCC.GetRowCellValue(gvCC.FocusedRowHandle, "TenNhomNL")
    End Sub
End Class
